﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Cloud.Dialogflow.V2;

namespace Bot.Domain.DialogFlow
{
    public class MatchModeStringEnumConverter : Newtonsoft.Json.Converters.StringEnumConverter
    {
        //public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        //{
        //    if (value is MatchMode)
        //    {
        //        writer.WriteValue(Enum.GetName(typeof(MatchMode), (MatchMode)value));// or something else
        //        return;
        //    }

        //    base.WriteJson(writer, value, serializer);
        //}
        
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (objectType == typeof(Agent.Types.MatchMode))
            {
                var enumString = reader.Value.ToString();
                string correctEnumString = enumString;
                switch (enumString)
                {
                    case "MATCH_MODE_HYBRID": correctEnumString = Agent.Types.MatchMode.Hybrid.ToString(); break;
                    case "MATCH_MODE_ML_ONLY": correctEnumString = Agent.Types.MatchMode.MlOnly.ToString(); break;
                    case "MATCH_MODE_UNSPECIFIED": correctEnumString = Agent.Types.MatchMode.Unspecified.ToString(); break;
                }
                return (Agent.Types.MatchMode)System.Enum.Parse(typeof(Agent.Types.MatchMode), correctEnumString);
            }
            else if (objectType == typeof(Intent.Types.WebhookState))
            {
                var enumString = reader.Value.ToString();
                string correctEnumString = enumString;
                switch (enumString)
                {
                    case "WEBHOOK_STATE_ENABLED": correctEnumString = Intent.Types.WebhookState.Enabled.ToString(); break;
                    case "WEBHOOK_STATE_ENABLEDFORSLOTFILLING": correctEnumString = Intent.Types.WebhookState.EnabledForSlotFilling.ToString(); break;
                    case "WEBHOOK_STATE_UNSPECIFIED": correctEnumString = Intent.Types.WebhookState.Unspecified.ToString(); break;
                }
                return (Intent.Types.WebhookState)System.Enum.Parse(typeof(Intent.Types.WebhookState), correctEnumString);
            }
            else if (objectType == typeof(EntityType.Types.Kind))
            {
                var enumString = reader.Value.ToString();
                string correctEnumString = enumString;
                switch (enumString)
                {
                    case "KIND_LIST": correctEnumString = EntityType.Types.Kind.List.ToString(); break;
                    case "KIND_MAP": correctEnumString = EntityType.Types.Kind.Map.ToString(); break;
                    case "KIND_UNSPECIFIED": correctEnumString = EntityType.Types.Kind.Unspecified.ToString(); break;
                }
                return (EntityType.Types.Kind)System.Enum.Parse(typeof(EntityType.Types.Kind), correctEnumString);
            }
            else if (objectType == typeof(EntityType.Types.AutoExpansionMode))
            {
                var enumString = reader.Value.ToString();
                string correctEnumString = enumString;
                switch (enumString)
                {
                    case "AUTOEXPANSIONMODE_DEFAULT": correctEnumString = EntityType.Types.AutoExpansionMode.Default.ToString(); break;
                    case "AUTOEXPANSIONMODE_UNSPECIFIED": correctEnumString = EntityType.Types.AutoExpansionMode.Unspecified.ToString(); break;
                }
                return (EntityType.Types.AutoExpansionMode)System.Enum.Parse(typeof(EntityType.Types.AutoExpansionMode), correctEnumString);
            }

            return base.ReadJson(reader, objectType, existingValue, serializer);
        }
    }
}
